package xuegao.study.mall.rbac.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.common.api.CommonPage;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.model.ums.UmsMenu;
import xuegao.study.mall.common.model.ums.UmsResource;
import xuegao.study.mall.common.model.ums.UmsRole;
import xuegao.study.mall.rbac.service.UmsRoleService;

import java.util.List;
import java.util.Map;

@Api(tags = "UmsRoleController")
@Tag(name = "UmsRoleController", description = "角色管理相关接口")
@RestController
@RequestMapping(value = "/role")
public class UmsRoleController {

    @Autowired
    private UmsRoleService umsRoleService;

    @ApiOperation("增加角色")
    @PostMapping("/add")
    public CommonResult<Integer> add(@RequestBody UmsRole umsRole) {
        int count = umsRoleService.insertRole(umsRole);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("更新角色")
    @PostMapping("/update/{id}")
    public CommonResult<Integer> update(@PathVariable("id") Long id, @RequestBody UmsRole umsRole) {
        int count = umsRoleService.updateRole(id, umsRole);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Integer> deleteRole(@PathVariable("id") Long id) {
        int count = umsRoleService.deleteById(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("角色列表查询")
    @GetMapping("/list")
    public CommonResult<CommonPage<UmsRole>> listRole(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "name", defaultValue = "") String name){
        List<UmsRole> list = umsRoleService.selectList(pageNum, pageSize, name);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("查询所有角色")
    @GetMapping("/listAll")
    public CommonResult<List<UmsRole>> listAll() {
        List<UmsRole> list = umsRoleService.listAll();
        return CommonResult.success(list);
    }

    @ApiOperation("根据adminId获取角色列表")
    @GetMapping("/getAdminRoles")
    public List<UmsRole> getRoleByUserId(@RequestParam Long adminId) {
        return umsRoleService.getRolesListByAdminId(adminId);
    }

    @ApiOperation("查询该角色菜单列表")
    @GetMapping("/listMenu/{roleId}")
    public CommonResult<List<UmsMenu>> listMenu(@PathVariable("roleId") Long roleId) {
        List<UmsMenu> umsMenus = umsRoleService.listMenuByRoleId(roleId);
        return CommonResult.success(umsMenus);
    }

    @ApiOperation("查询该角色资源列表")
    @GetMapping("/listResource/{roleId}")
    public CommonResult<List<UmsResource>> listResource(@PathVariable("roleId") Long roleId) {
        List<UmsResource> umsResources = umsRoleService.listResourceByRoleId(roleId);
        return CommonResult.success(umsResources);
    }

    @ApiOperation("给角色分配菜单")
    @PostMapping("/allocMenu")
    public CommonResult<Integer> allocMenu(@RequestParam Long roleId, @RequestParam List<Long> menuIds) {
        int count = umsRoleService.allocMenu(roleId, menuIds);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("给角色分配资源")
    @PostMapping("/allocResource")
    public CommonResult<Integer> allocResource(@RequestParam Long roleId, @RequestParam List<Long> resourceIds) {
        int count = umsRoleService.allocResource(roleId, resourceIds);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("角色资源映射列表")
    @GetMapping("/initResourceRolesMap")
    public CommonResult<Map<String, List<String>>> initResourceRolesMap() {
        Map<String, List<String>> resultMap = umsRoleService.initResourceRolesMap();
        return CommonResult.success(resultMap);
    }
}
